package bean;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import beans.CasaDAORemote;
import beans.CasaDTO;
import beans.ProprietateDAORemote;
import beans.ProprietateDTO;
import beans.TerenDAORemote;
import beans.TerenDTO;
import beans.UtilStatelessRemote;


@ManagedBean
@RequestScoped
public class ProprietateBeanController {

	@EJB
	UtilStatelessRemote utilEJB;

	@EJB
	CasaDAORemote casaDAO;

	@EJB
	ProprietateDAORemote proprietateDAO;

	@EJB
	TerenDAORemote terenDAO;

	ProprietateDTO proprietateDTO;
	CasaDTO casaDTO;
	TerenDTO terenDTO;

	String radio;
	int editable;
	String type;
	int view = 1;

	List<ProprietateDTO> propList;

	List<CasaDTO> caseList;

	List<TerenDTO> terenList;

	// ProprietateBean pb;

	public String inserareProprietate() {
		proprietateDAO.insertProperty(proprietateDTO);
		return "ok";
	}

	public String inserareCasa() {
		casaDAO.insertCasa(casaDTO);
		return "ok";
	}

	public String inserareTeren() {
		terenDAO.insertTeren(terenDTO);
		return "ok";
	}

	public ProprietateDTO getProprietateDTO() {
		return proprietateDTO;
	}

	public void setProprietateDTO(ProprietateDTO proprietateDTO) {
		this.proprietateDTO = proprietateDTO;
	}

	public CasaDTO getCasaDTO() {
		return casaDTO;
	}

	public void setCasaDTO(CasaDTO casaDTO) {
		this.casaDTO = casaDTO;
	}

	public TerenDTO getTerenDTO() {
		return terenDTO;
	}

	public void setTerenDTO(TerenDTO terenDTO) {
		this.terenDTO = terenDTO;
	}

	public String getRadio() {
		return radio;
	}

	public void setRadio(String radio) {
		this.radio = radio;

		switch (radio) {
		case "prop":
			editable = 1;
			break;

		case "casa":
			editable = 2;
			break;

		case "teren":
			editable = 3;
			break;

		}
	}

	public int getEditable() {
		return editable;
	}

	public void setEditable(int editable) {
		this.editable = editable;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;

		switch (type) {
		case "add":
			view = 1;
			break;
		case "view":
			view = 2;
			break;
		}
	}

	public int getView() {
		return view;
	}

	public void setView(int view) {
		this.view = view;
	}

	public List<CasaDTO> getCaseList() {
		return casaDAO.getCasaList();
	}

	public List<TerenDTO> getTerenList() {
		return terenDAO.getTerenList();
	}

	public List<ProprietateDTO> getPropList() {
		return proprietateDAO.getProprietateList();
	}

	public void setPropList(List<ProprietateDTO> propList) {
		this.propList = propList;
	}

	public void setCaseList(List<CasaDTO> caseList) {
		this.caseList = caseList;
	}

	public void setTerenList(List<TerenDTO> terenList) {
		this.terenList = terenList;
	}

	public String propSortAdress() {
			
		List<ProprietateDTO> plist = getPropList();
		List<ProprietateDTO> newList = new ArrayList<>();
		
		ProprietateDTO min;
		while(!plist.isEmpty())
		{
			min= plist.get(0);
			
			for(ProprietateDTO p:plist)
			{
				if(min.getAdresa().compareToIgnoreCase(p.getAdresa())>0)
				{
					min=p;
				}
			}
			
			newList.add(min);
			plist.remove(min);
		}
		
		setPropList(newList);
		
		return "ok";
	}
	
	public String casaSortAdress() {
		List<CasaDTO> plist = getCaseList();
		List<CasaDTO> newList = new ArrayList<>();
		
		CasaDTO min;
		while(!plist.isEmpty())
		{
			min= plist.get(0);
			
			for(CasaDTO p:plist)
			{
				if(min.getAdresa().compareToIgnoreCase(p.getAdresa())>0)
				{
					min=p;
				}
			}
			
			newList.add(min);
			plist.remove(min);
		}
		
		setCaseList(newList);
		
		return null;
	}
	
	public String casaSortNrCamere() {
		List<CasaDTO> plist = getCaseList();
		List<CasaDTO> newList = new ArrayList<>();
		
		CasaDTO min;
		while(!plist.isEmpty())
		{
			min= plist.get(0);
			
			for(CasaDTO p:plist)
			{
				if(min.getNrcamere()>p.getNrcamere())
				{
					min=p;
				}
			}
			
			newList.add(min);
			plist.remove(min);
		}
		
		setCaseList(newList);
		
		return "ok";
	}
	
	public String casaSortPret() {
		Collections.sort(getCaseList(), new Comparator<CasaDTO>() {

			@Override
			public int compare(CasaDTO p1, CasaDTO p2) {
				if(p1.getPret()<p2.getPret())
				{
					return -1;
				}
				else{
					if(p1.getPret()==p2.getPret())
					{
						return 0;
					}
					else{
						
							return 1;
					}}
				
			}
		});
		return "ok";
	}
	
	public String casaSortSuprafata() {
		Collections.sort(getCaseList(), new Comparator<CasaDTO>() {

			@Override
			public int compare(CasaDTO p1, CasaDTO p2) {
				if(p1.getSuprafata()<p2.getSuprafata())
				{
					return -1;
				}
				else{
					if(p1.getSuprafata()==p2.getSuprafata())
					{
						return 0;
					}
					else{
						
							return 1;
					}}
				
			}
		});
		return "ok";
	}
	
	public String terenSortAdress() {
		Collections.sort(getTerenList(), new Comparator<TerenDTO>() {

			@Override
			public int compare(TerenDTO p1, TerenDTO p2) {
				return p1.getAdresa().compareToIgnoreCase(p2.getAdresa());
			}
		});
		return "ok";
	}
	
	public String terenSortPretUnitar() {
		Collections.sort(getTerenList(), new Comparator<TerenDTO>() {

			@Override
			public int compare(TerenDTO p1, TerenDTO p2) {
				if(p1.getPretunitar()<p2.getPretunitar())
				{
					return -1;
				}
				else{
					if(p1.getPretunitar()==p2.getPretunitar())
					{
						return 0;
					}
					else{
						
							return 1;
					}}
				
			}
		});
		return "ok";
	}
	
	public String terenSortSuprafata() {
		Collections.sort(getTerenList(), new Comparator<TerenDTO>() {

			@Override
			public int compare(TerenDTO p1, TerenDTO p2) {
				if(p1.getSuprafata()<p2.getSuprafata())
				{
					return -1;
				}
				else{
					if(p1.getSuprafata()==p2.getSuprafata())
					{
						return 0;
					}
					else{
						
							return 1;
					}}
				
			}
		});
		return "ok";
	}
}
